package com.wsjc.common.solr.factory;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import com.wsjc.common.core.config.SolrConfig;

/**
 * 功能说明：solr 客户端工厂类 , 单节点/集群
 * @author 93733
 *
 */

public class SolrServerFactory {

	volatile private static SolrServerFactory solrServerFactory ;
	
	private SolrServerFactory(){
	}
	
	public static synchronized SolrServerFactory getInstance(){
		
		if(null == solrServerFactory){
			solrServerFactory = new SolrServerFactory() ;
		}
		return solrServerFactory ;
		
	}
	
	public SolrServer getSolrServerInstance(String code){
		
		SolrServer solrServer = null ;
		switch (code) {
			case "solr":
				String solrAddress = SolrConfig.readConfig("solr.address") ;
				solrServer = new HttpSolrServer(solrAddress);
				break;
			case "solrCluster":
				String zkHost = SolrConfig.readConfig("zk.hosts") ;
				solrServer = new CloudSolrServer(zkHost);
				((CloudSolrServer) solrServer).setDefaultCollection("solr.conlections");
				break;
			default:
				break;
		}
		return solrServer ;
	}
	
}
